package com.example.administrator.runbook.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.administrator.runbook.R;
import com.example.administrator.runbook.findfragment.activity.AudioBooksActivity;
import com.example.administrator.runbook.findfragment.activity.FindClassifyActivity;
import com.example.administrator.runbook.findfragment.adapter.MyBaseAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FindFragment extends CommonFragment implements AdapterView.OnItemClickListener {

    private boolean hasMeasured;
    private int mHeight;
    private ListView mView;
    private List<String> mObjects;


    public FindFragment() {
        // Required empty public constructor
    }

    public String getTitle() {
        return "发现";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_find, container, false);
        mView = (ListView) inflate.findViewById(R.id.fragment_find_list);
        ViewTreeObserver observer = mView.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (hasMeasured == false) {
                    mHeight = mView.getHeight();
                    setListAdapter();
                    hasMeasured = true;
                }
                return true;
            }
        });
        return inflate;
    }

    private void setListAdapter() {
        mObjects = new ArrayList<>();
        String[] array = getResources().getStringArray(R.array.findItem);
        Collections.addAll(mObjects, array);
        MyBaseAdapter adapter = new MyBaseAdapter(mObjects, getContext(), mHeight);
        mView.setAdapter(adapter);
        mView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String s = mObjects.get(position);
        Intent mIntent;
        switch (s) {
            case "排行榜":
                break;
            case "分类":
                mIntent = new Intent(getContext(), FindClassifyActivity.class);
                startActivity(mIntent);
                break;
            case "主题书单":
                break;
            case "有声小说":
                mIntent = new Intent(getContext(), AudioBooksActivity.class);
                startActivity(mIntent);
                break;
            case "神秘功能":
                break;
            case "漫画中心":
                break;
            case "游戏中心":
                break;
            case "咪咕阅读":
                break;
            case "一元得宝":
                break;
            case "情感热线":
                break;
        }
    }
}
